﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rubik.Common.Operations
{
    public class TopRotateClockwise : ICubeOperation
    {
        private readonly int level;

        public TopRotateClockwise(int level)
        {
            this.level = level;
        }

        private readonly ElementPositionTransform[,] transformationMatrix = new[,]
            {
                { new ElementPositionTransform(2, 0, 0), new ElementPositionTransform(1, 0, 1), new ElementPositionTransform(0, 0, 2) },
                { new ElementPositionTransform(1, 0, -1), new ElementPositionTransform(0, 0, 0), new ElementPositionTransform(-1, 0, 1) },
                { new ElementPositionTransform(0, 0, -2), new ElementPositionTransform(-1, 0, -1), new ElementPositionTransform(-2, 0, 0) }
            };

        public void Do(Cube cube)
        {
            CubeElement[,,] copy = (CubeElement[,,])cube.Elements.Clone();
            for (int i = 0; i < Cube.Scale; i++)
            {
                for (int j = 0; j < Cube.Scale; j++)
                {
                    int xShift = transformationMatrix[j, i].XDelta;
                    int zShift = transformationMatrix[j, i].ZDelta;
                    copy[i + xShift, level, Cube.Scale - 1 - j - zShift] = cube.Elements[i, level, Cube.Scale - 1 - j];
                    copy[i + xShift, level, Cube.Scale - 1 - j - zShift].Rotate(RotateAxis.Y, true);
                }
            }
            cube.Elements = copy;
        }
    }
}
